package com.weimob.mcloud.wechatwork.customer.service.user.utils;

import com.alibaba.fastjson.JSON;
import com.weimob.dubbo.generic.bean.response.ErrorCode;
import com.weimob.mcloud.wechatwork.customer.service.user.base.PaasBaseResponse;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.zipkin.ZipkinContext;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by wufeng on 2016/9/7.
 * 判断dubbo 业务调用是否正常
 */
@Slf4j
public class PaasResponseUtil {

    public static <T> PaasBaseResponse<T> ok(SoaResponse<T, ?> soaResponse) {
        PaasBaseResponse<T> paasBaseResponse = new PaasBaseResponse<>();

        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("PaasBaseResponse error, ticket:{}, response:{}, ",
                    ZipkinContext.getContext().getGlobalTicket(),JSON.toJSONString(soaResponse));
        }

        paasBaseResponse.setData(soaResponse.getResponseVo());
        paasBaseResponse.setCodeByErrorCode(soaResponse.getReturnCode(), soaResponse.getReturnMsg());
        return paasBaseResponse;
    }

    public static <T> PaasBaseResponse<T> success(T data) {
        PaasBaseResponse<T> paasBaseResponse = new PaasBaseResponse<>();
        paasBaseResponse.setData(data);
        paasBaseResponse.setCodeByErrorCode(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg());
        return paasBaseResponse;
    }

    public static <T> PaasBaseResponse<T> error(String returnCode, String returnMsg, T data) {
        PaasBaseResponse<T> paasBaseResponse = new PaasBaseResponse<>();
        paasBaseResponse.setData(data);
        paasBaseResponse.setCodeByErrorCode(returnCode, returnMsg);
        return paasBaseResponse;
    }
}
